#include "CCustomNumpad.h"
#include "ui_CCustomNumpad.h"

CCustomNumpad::CCustomNumpad(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CCustomNumpad)
{
    ui->setupUi(this);

    connect(ui->btn_key_0, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_1, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_2, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_3, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_4, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_5, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_6, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_7, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_8, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_9, &QPushButton::clicked, this, &CCustomNumpad::on_key_clicked);
    connect(ui->btn_key_del, &QPushButton::clicked, this, &CCustomNumpad::del);
    connect(ui->btn_key_ok, &QPushButton::clicked, this, &CCustomNumpad::ok);

    setFocusPolicy(Qt::NoFocus);
    ui->btn_key_0->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_1->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_2->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_3->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_4->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_5->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_6->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_7->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_8->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_9->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_del->setFocusPolicy(Qt::NoFocus);
    ui->btn_key_ok->setFocusPolicy(Qt::NoFocus);
}

CCustomNumpad::~CCustomNumpad()
{
    delete ui;
}

void CCustomNumpad::enableKeyOK(bool flag)
{
    ui->btn_key_ok->setEnabled(flag);
    ui->btn_key_ok->setText("OK");
}

void CCustomNumpad::on_key_clicked()
{
    auto obj = this->sender();
    QPushButton* btn = qobject_cast<QPushButton*>(this->sender());
    if (btn == nullptr)
    {
        return;
    }

    auto key_str = btn->text().trimmed();

    emit key(key_str);
}
